<?php
/**
 * @software         [JiJinCMS]
 * @author           [上海集锦信息科技有限公司]
 * @website          [http://www.jijinweb.com]
 * @email            [sunny@jijinweb.com]
 **/
namespace app\admin\controller;
use think\facade\View;
use app\admin\model\RoleModel;
use think\facade\Db;
use think\facade\Request;
class FeedbackCat extends \app\admin\controller\Admin{
    public function __construct(\think\App $app) {
        parent::__construct($app);
        $this->model=new \app\admin\model\FeedbackCatModel();
        $this->validate=new \app\admin\validate\FeedbackCatValidate();
        View::assign("memberGroup",Db::name("member_group")->select());
        //展示该数据表下所有的字段
        View::assign("table_field",Db::name("feedback_field")->order("sort asc,field_id asc")->select());
    }    
    //
    public function index(){
        $data=$this->model->order("sort asc,catid asc")->select();  
        View::assign('data', $data);
        return View::fetch();
    }
    //
    public function add(){
        if(request()->isPost()){
            $data=$this->request->param();
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            }
            $data["is_enter"]=$this->model->arr2string(Request::param('is_enter'));
            $data["must_enter"]=$this->model->arr2string(Request::param('must_enter'));
            $data["member_group"]=$this->model->arr2string(Request::param('member_group'));
            $data["formtemp"]=$this->model->get_formtemp($data);
            $this->model->save($data);
            $catid=$this->model->catid;
            $this->model->updateModelTemp($catid);//更新后台模型缓存
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$catid,$this->request->param());            
            $this->success($catid);
        }else{
            return View::fetch();
        }
    }
    public function update(){
        if(request()->isPost()){
            $data=$this->request->param();
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            }
            $data["is_enter"]=$this->model->arr2string(Request::param('is_enter'));
            $data["must_enter"]=$this->model->arr2string(Request::param('must_enter'));
            $data["member_group"]=$this->model->arr2string(Request::param('member_group'));
            $data["formtemp"]=$this->model->get_formtemp($data);
            $this->model->update($data);
            $this->model->updateModelTemp($data['catid']);//更新后台模型缓存
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$data['catid'],$this->request->param());            
            $this->success($catid);
        }else{
            $catid=Request::param('catid',0,"intval");
            $r=$this->model->find($catid);
            View::assign("r",$r);
            //将formtemp中的字段名称更新到字段列表中
            $field_name_arr=array();
            $field_arr=array();
            $formtype_arr=explode("\r\n",$r["formtemp"]);
            foreach($formtype_arr as $v){
                $f=explode("<!--field-->",$v);
                if(isset($f[1])){
                    $field_arr[]=$f[1];
                    $field_name_arr[$f[1]]=$f[0];
                }
            }
            $new_table_field=array();
            //展示该数据表下所有的字段
            $table_field=Db::name("feedback_field")->order("sort asc,field_id asc")->select();  
            foreach($table_field as $v){
                //更新字段名
                if(in_array($v["field"], $field_arr)){
                    $v["name"]=$field_name_arr[$v["field"]];
                }
                //判断该字段分别在每个勾选项里是否有勾选
                $v["is_enter_checked"]=strstr($r['is_enter'],",".$v["field"].",")?1:0;
                $v["must_enter_checked"]=strstr($r['must_enter'],",".$v["field"].",")?1:0;
                //
                $new_table_field[]=$v;
            } 
            View::assign("table_field",$new_table_field);            
            return View::fetch();
        }
    }    
    //设置状态是否启用
    public function setStatus(){
        $catid=Request::param('catid',0,"intval");
        $status=Request::param('status',0,"intval");
        $this->model->update(['catid'=>$catid,'status'=>$status]);
        //记录日志
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$catid);        
        $this->success("设置成功");
    }    
    //设置状态是否启用
    public function setVerifycode(){
        $catid=Request::param('catid',0,"intval");
        $status=Request::param('status',0,"intval");
        $this->model->update(['catid'=>$catid,'verify_code'=>$status]);
        //记录日志
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$catid);        
        $this->success("设置成功");
    }    
    //设置状态是否启用
    public function setMobileverifycode(){
        $catid=Request::param('catid',0,"intval");
        $status=Request::param('status',0,"intval");
        $this->model->update(['catid'=>$catid,'mobile_verify_code'=>$status]);
        //记录日志
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$catid);        
        $this->success("设置成功");
    } 
    //字段排序
    public function updateSort(){
        $catids=Request::param("ids");
        $sort=Request::param("sort");
        foreach($catids as $key=>$catid){
            $data['catid']=(int)$catid;
            $data['sort']=(int)$sort[$key];
            $this->model->update($data);
        }
        $this->success("排序更改成功");
    }   
    public function delete(){
        $id=Request::param("catid",0,"intval");
        if(!$id){
             $this->error=('请选择信息！');
        }
        $this->model->destroy($id);
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$id,$this->request->param());  
        $this->success("删除成功");
    }    
}
